import '../../style/resource.less';
import React, {
    Component
} from 'react'
import {
    inject,
    observer
} from 'mobx-react';
import {
    withRouter
} from 'react-router-dom';
import Header from './Header';
import Catelog from './Catelog';
import Info from './Info';
import ButtonBar from './ButtonBar';
import { Icon,Tools } from 'weaver-mobile';
const getLabel = Tools.getLabel;

@inject('resourceStore')
@withRouter
@observer
export default class ResourceInfo extends Component {
    constructor(props) {
        super(props);
    }

    componentWillMount() {
        if (!window.em) return;
        const {
            resourceStore: store,
        } = this.props, {
            JSAPI
        } = store;
        const results = [];
        JSAPI.forEach(v => {
            results.push(window.em.checkJsApi(v))
        });
        store.setCheckJsApiResults(results);
    }

    componentWillReceiveProps(nextProps) {
        const {
            match: {
                params: {
                    id
                }
            }
        } = nextProps;
        this.props.resourceStore.queryResourceInfo(id);
    }

    componentDidMount() {
        const {
            resourceStore: store,
            match: {
                params: {
                    id
                }
            },
            location,
            history,
        } = this.props;
        
        store.getResourceBaseTitle(id).then(() => {
            store.queryResourceInfo(id, '1');
        });

        if (location.search && location.search.indexOf("from=flow") > -1) {
            // this.rewriteBack(history);
        }
    }

    rewriteBack = (history) => {
        const em = window.em;
        if (em && em.checkJsApi("configNavigation")) {
            em.configNavigation({
                left:{
                    name:getLabel(1290, "返回"),
                    action:() => {
                        window.history.go(-2);
                    }
                },
                showCloseButton:false,
            })
        }
    }

    componentWillUnmount() {
        const {
            resourceStore: store,
        } = this.props;
        store.resetDatas();
    }

    render() {
        const {
            resourceStore: store
        } = this.props, {
            datas,
            hasRight,
            authloading,
        } = store, {
            bgData
        } = datas;

        if (authloading) {
            return null;
        }

        if (!hasRight) {
            return (
                <div className="hrm-auth">
                    <Icon type="wuquanxian" size="lg"/>
                    <div>{getLabel(512047,"暂无权限")}</div>
                </div>
            );
        }

        return (
            <div className='resource'>
                <div className='content' style={{height:(bgData.length === 0)&&'100%'}}>
                    <Header store={store} />
                    <Catelog store={store} />
                    <Info store={store} />
                </div>
                <ButtonBar store={store} />
            </div>
        )
    }
}